package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.WareHouse;
import com.zhou.mapper.WareHouseMapper;
import com.zhou.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WareHouseServiceImpl implements WareHouseService {
    @Autowired
    private WareHouseMapper wareHouseMapper;

    /**
     * 仓库库存查询
     *
     * @param page     1
     * @param pageSize 10
     * @return data
     */
    @Override
    public List<WareHouse> getAll(Integer page, Integer pageSize, String expireTime) {
        Page<WareHouse> pg = new Page<>();
        LambdaQueryWrapper<WareHouse> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(expireTime)) {
            wrapper.eq(WareHouse::getExpireTime, expireTime);
        }
        Page<WareHouse> wareHousePage = wareHouseMapper.selectPage(pg, wrapper);
        if (StringUtils.isEmpty(wareHousePage)) {
            throw new RuntimeException("没有查到数据，请重试");
        }
        List<WareHouse> records = wareHousePage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            throw new RuntimeException("仓库空啦，请联系管理员");
        }
        return records;
    }

    /**
     * 专门用来修改物品过期时间
     *
     * @param expireTime 新的过期时间
     * @return
     */
    @Override
    public Boolean getByTime(String expireTime, String wareHouseId) {
        //判断入参时间是否大于当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        //时间格式转换进行对比，如果接近需要返回给前端
        Date parse = null;
        Date expireTime1 = null;
        try {
            parse = dateFormat.parse(format);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        if (parse.compareTo(expireTime1) > 0) {
            throw new RuntimeException("参数时间小于现在的时间，请重试");
        }
        WareHouse wareHouse = wareHouseMapper.selectById(wareHouseId);
        if (StringUtils.isEmpty(wareHouse)) {
            throw new RuntimeException("没有查到数据，请重试");
        }
        wareHouse.setExpireTime(parse);
        int i = wareHouseMapper.updateById(wareHouse);
        if (i == 0) {
            throw new RuntimeException("修改失败，请重试");
        }
        return true;
    }

    /**
     * 定期查询过期时间是否过期
     *
     * @return
     */
    @Override
    public List<WareHouse> getTimeAll() {
        Date date = new Date();
        try {
            //获取到数据库中的过期时间进行对比
            LambdaQueryWrapper<WareHouse> wrapper = new LambdaQueryWrapper<>();
            List<WareHouse> wareHouses = wareHouseMapper.selectList(wrapper);
            List<WareHouse> list = new ArrayList<>();
            for (WareHouse wareHouse : wareHouses) {
                int i = wareHouse.getExpireTime().compareTo(date);
                if (i == -1) {
                    //把所有符合条件都加到集合返回
                    list.add(wareHouse);
                }
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
